import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//屏蔽重复使用路由
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function (location) {
    return originalReplace.call(this, location).catch(err => err)
}

import Msite from '@/pages/Msite/Msite'
/* 按需加载其他路由组件 */
const Search = () => import('@/pages/Search/Search')
const Order = () => import('@/pages/Order/Order')
const OrderDetail = () => import('@/pages/OrderDetail/OrderDetail')
const Login = () => import('@/pages/Login/Login')
const Profile = () => import('@/pages/Profile/Profile')
const Personal = () => import('@/pages/Profile/Personal/Personal')
const Shop = () => import('@/pages/Shop/Shop')
const ShopGoods = () => import('@/pages/Shop/ShopGoods/ShopGoods')
const ShopRatings = () => import('@/pages/Shop/ShopRatings/ShopRatings')
const Account = () => import('@/pages/Account/Account')

const router = new VueRouter({
   routes: [
      {
         path: '/',
         redirect: '/msite'
      },
      {
         path: '/login',
         component: Login,
         meta: {
            title: '登录',
            tableBar: false
         }
      },
      {
         path: '/msite',
         component: Msite,
         meta: {
            title: '首页',
            tableBar: true
         }
      },
      {
         path: '/search',
         component: Search,
         meta: {
            title: '搜索',
            tableBar: true
         }
      },
      {
         path: '/order',
         component: Order,
         meta: {
            title: '订单',
            tableBar: true
         }
      },
      {
         path: '/orderdetail/:orderid',
         component: OrderDetail,
         meta: {
            title: '订单详情',
            tableBar: false
         }
      },
      {
         path: '/profile',
         component: Profile,
         meta: {
            title: '我的',
            tableBar: true
         }
      },
      {
         path: '/personal',
         component: Personal,
         meta: {
            title: '个人详情',
            tableBar: false
         }
      },
      {
         path: '/shop/:id',
         component: Shop,
         meta: {
            title: '商品详情',
            tableBar: false
         },
         children: [
            {
               path: '/shop/:id/goods',
               component: ShopGoods,
               meta: {
                  title: '店铺'
               }
            },
            {
               path: '/shop/:id/ratings',
               component: ShopRatings,
               meta: {
                  title: '评价'
               }
            }
         ]
      },
      {
         path: '/address/:id',
         component: Account,
         meta: {
            title: '结算页',
            tableBar: false
         }
      }
   ]
})

/* 配置全局路由守卫 */
router.beforeEach((to, from, next) => {
   next()
})
router.afterEach((to, from) => {
   document.title = to.meta.title || ''
})

export default router